from django.http import HttpResponse
import json
import requests
from bs4 import BeautifulSoup
import re

#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")

    lis = soup.select(".listCont tbody tr")

    for li in lis:
        tempData = resultData()
        str = li.select("td")[4].text.strip()
        try:
            tempData["titleName"] = li.select(".title")[0].text.strip()
        except:
            try:
                tempData["titleName"] = li.select("td")[2].text.strip()
            except:
                pass

        try:
            tempData["titleHref"] = 'http://ref.cnki.net'+li.select("title")[0]["href"]
        except:
            pass
        try:
            tempData["author"] = [li.select("td")[3].text.strip()]
        except:
            pass
        try:
            tempData["source"] = li.select("td")[4].text.strip()
        except:
            pass
        try:
            tempData["publishTime"] = li.select("td")[5].text.strip()
        except:
            pass

        allData.append(tempData)

    return allData

def getData(keyword):
    headers = {
        'Accept': '*/*',
        'Referer': 'http://ref.cnki.net/REF/AdvSearch/Index?colName=%E8%A2%AB%E5%BC%95%E4%B8%BB%E9%A2%98&colValue=cell&isJump=true',
        'Origin': 'http://ref.cnki.net',
        'X-Requested-With': 'XMLHttpRequest',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    }

    data = {
        '$oldSql': '',
        'dbId': 'cb_beref_default',
        'beRefCountCond': '',
        'articleCond': ' ( (\u4E3B\u9898 %'+keyword+') ) ',
        'authorCond': '',
        'publishTimeCond': '',
        'refTimeCond': '',
        'srcCond': '',
        'fundCond': '',
        'catalogCond': '',
        'pageIndex': '0',
        'pageSize': '50',
        'displayMode': 'list',
        'inputSql': '',
        'articleKeyword': keyword+';',
        'authorKeyword': ';',
        'srcKeyword': ';',
        'orderBy': 'relevant',
        'operate': 'search',
        'cjfdType': '',
        'issue': ''
    }
    proxies = {
        "http": "http://qf:qf3.1415@202.112.118.7:10086/"
    }
    response = requests.post('http://ref.cnki.net/REF/AdvSearch/GetAdvSearchResult', headers=headers, data=data,proxies=proxies)

    allData = formData(response)

    return allData


def index(request):

    data = json.loads(request.POST.get('data', '1'))

    # return HttpResponse([data], content_type="application/json")

    allData= getData(data["values"][0])

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")